from django.shortcuts import render,HttpResponse,redirect,HttpResponsePermanentRedirect
from rest_framework.views import APIView
from rest_framework.response import Response
from captcha.image import ImageCaptcha
import jwt,redis,random,json,requests
import string,re,time
from zbj.my_redis import r
from zbj.models import *
from zbj.sms_code import sms_code
from zbj.my_jwt import jj

# 获取权限
def Get_resource(id):
    list = []
    list1 = []
    list2 = []
    res = Role_resource.objects.get(role_id_id=id)
    res2 = Resource.objects.filter(id=res.resource_id_id)  # 3
    for i in res2:
        list2.append({'id': i.id})
    list2 = list2[0]
    res3 = Resource.objects.filter(uid=list2["id"])
    for i in res2:
        for j in res3:
            list1.append({
                "id": j.id,
                'name': j.name,
                "url": j.url
            })
        list.append({
            "id": i.id,
            'name': i.name,
            "son": list1
        })
    return list

import snowflake.client
#封装雪花算法
def create_ordercode(tablename,count):
    #生成唯一id
    code = snowflake.client.get_guid()
    # 使用redis进行回拨
    value = r.get_str(tablename)
    if value:
        if int(code) < int(value):
            create_ordercode(tablename,count)
    #如果redis中没有，就进行存储
    r.set_str(tablename,code)
    # 取模块到列表里
    print('hash(code)',hash(code))
    number = hash(code) % count
    if number == 0:
        return tablename,code
    else:
        return tablename + str(number),code